﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using SISIN.BE;

namespace SISIN.DA
{
    public class UsuarioDA
    {
        #region SINGLETON

        private static UsuarioDA _instance = null;
        /// <summary>
        /// Devuelve una instancia del objeto
        /// </summary>
        /// <remarks></remarks>
        public static UsuarioDA GetInstance()
        {

            if ((_instance == null))
            {
                _instance = new UsuarioDA();
            }

            return _instance;

        }

        #endregion

        public UsuarioBE GetById(int id) {
            UsuarioBE usuarioInfo = null;
            try
            {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_USUARIO_GET_ID");

                db.AddInParameter(cmd, "@n_usuario_id", DbType.Int32, id);

                IDataReader dr = db.ExecuteReader(cmd);

                if (dr.Read())
                {
                    usuarioInfo = new UsuarioBE();
                    usuarioInfo.Id = dr.GetInt32(dr.GetOrdinal("n_usuario_id"));
                    usuarioInfo.Nickname = dr.GetString(dr.GetOrdinal("s_usu_nickname"));
                    usuarioInfo.Online = dr.GetBoolean(dr.GetOrdinal("b_usu_estado"));
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return usuarioInfo;
        }

        public int GetAccess(String username, String password) {
            int id = 0;

            try {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_USUARIO_LOGIN");
                
                db.AddOutParameter(cmd, "@n_usu_id", DbType.Int32, 4);
                db.AddInParameter(cmd, "@s_usu_nickname", DbType.String, username);

                db.ExecuteNonQuery(cmd);

                id = Convert.ToInt32(db.GetParameterValue(cmd, "@n_usu_id"));
            }
            catch (Exception ex) {
                throw;
            }

            return id;
        }

        public Boolean LogOut(int id){
            Boolean ok = false;

            try {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_USUARIO_LOGOUT");

                db.AddInParameter(cmd, "@n_usuario_id", DbType.Int32, id);
                db.ExecuteNonQuery(cmd);

                ok = true;
            }
            catch (Exception ex) {
                throw;
            }

            return ok;
        }

        public List<UsuarioBE> GetUsersOnline(){
            List<UsuarioBE> usuarios = new List<UsuarioBE>();

            try {
                Database db = DatabaseFactory.CreateDatabase("ConnectionString");
                DbCommand cmd = db.GetStoredProcCommand("PA_USUARIO_GET_ONLINE");

                using (IDataReader dr = db.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        UsuarioBE obj = new UsuarioBE();
                        obj.Id = dr.GetInt32(dr.GetOrdinal("n_usuario_id"));
                        obj.Nickname = dr.GetString(dr.GetOrdinal("s_usu_nickname"));
                        obj.Online = dr.GetBoolean(dr.GetOrdinal("b_usu_estado"));

                        usuarios.Add(obj);
                    }
                }
            }
            catch(Exception ex){
                throw;
            }

            return usuarios;
        }
    }
}
